package cn.com.gzheroli.rabbitmq.listener;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;

import org.springframework.amqp.core.Message;

import cn.com.gzheroli.core.common.service.CommonService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component
public class AsyncQueueListener {
	@Autowired
    private CommonService commonService;
	@Autowired
    private RabbitTemplate  template;


    /**
     * 同步队列
     * @param id 任务ID
     * @param type 任务名称
     */
    @RabbitListener(queues = "ccs.queue.sync")
    public void hello1(Object obj) {
        System.out.println("Received request for id " + obj.toString());
    }

    /**
     * 异步队列，SendTo为回复的队列名称
     * @param id 任务ID
     * @param type 任务名称
     * @return
     */
    @RabbitListener(queues = "ccs.queue.async")
    @SendTo("ccs.queue.async.reply")
    public Map hello2(Object obj) {
        //返回执行结果（成功，失败）和ID
        boolean success=true;
        Map result=new HashMap();
        result.put("success", success);
        result.put("id", "id");
        return result;
    }
    
    @RabbitListener(queues = "ccs.queue.async")
    @SendTo("ccs.queue.bds.reply")
    public Map hello3(Object obj) {
        //返回执行结果（成功，失败）和ID
        boolean success=true;
        Map result=new HashMap();
        result.put("success", success);
        result.put("id", "id");
        return result;
    }
    @RabbitListener(queues = "ccs.queue.async")
    @SendTo("ccs.queue.etl.reply")
    public Map hello4(Object obj) {
        //返回执行结果（成功，失败）和ID
        boolean success=true;
        Map result=new HashMap();
        result.put("success", success);
        result.put("id", "id");
        return result;
    }
    @RabbitListener(queues = "ccs.queue.log.async")
    @SendTo("ccs.queue.log.reply")
    public Map hello5(Object   obj) {
    	String routingKey="ccs.binding.log.async";
    	Message message=(Message)obj;
    	String type=message.getMessageProperties().getType();
    	Map headerMap=message.getMessageProperties().getHeaders();
    	JsonMessageConverter converter = new JsonMessageConverter();
    	Object resultObj = converter.fromMessage(message);
    	/*Gson gson=new Gson().toJson(obj);
    	gson.toJson(obj)*/
        //返回执行结果（成功，失败）和ID
        boolean success=true;
        Map result=new HashMap();
        result.put("success", success);
        //result.put("logObj", obj);
        commonService.save(resultObj);
        return result;
    }
    
}
